﻿@{
    ViewBag.Title = "Update";
    Layout = "~/Views/Shared/_AdminLayout.cshtml";
}


@section Header
{
    <script>
        function submit() {
            //编辑器对象
            var uEditor = UE.getEditor('txt_AdviderAbstract');

            //验证输入
            if ($("#txt_AdviserName").validatebox('isValid') == false) { return; }

            if (uEditor.hasContents() == false) {
                $.messager.alert('失败', '请输入内容', 'error');
                return;
            }


            //获取附件列表
            var attachmentString = "";
            //---------注意：这个地方有点恶心，冒号分割文件名和保存的的文件名，竖号分割每个附件，在实体类里面进行组装list
            $div = $('#AttarchmentDIV').children('div');
            $div.each(
                function () {
                    attachmentString += $(this).attr('fileName') + ':' + $(this).attr('saveName') + '|';
                }
            );


            //参数对象
            var adviser = {
                AdviserID: $("#hid_ID").val(),
                AdviserName: $("#txt_AdviserName").val(),
                Abstract: uEditor.getContent(),
                AttarchmentString: attachmentString
            };

            $.post("/Adviser/UpdateAdviser/", adviser,
                    function (data, textStatus) {
                        if (data == 'success') { $.messager.alert('成功', '修改成功'); }
                        else { $.messager.alert('失败', data, 'error'); }
                    }
                  );
        }

        function goback() {
            window.location.href = "/Adviser/Edit";
        }


        function removeAttarchment(divID) {
            $('#' + divID).remove();
        }

        //附件上传成功
        function onUploadSuccess(file, data, response) {
            eval("data=" + data);
            var fileId = data.FileID;
            $('#AttarchmentDIV').append('<div id="' + fileId + '" fileName = "' + file.name + '" saveName = "' + data.SaveName + '">' + file.name + '<a href = "#" onclick = "removeAttarchment(\'' + fileId + '\')">删除</a></div>');
        }
    </script>
}

<body>

    <div>
        @{ AdviserInfo adviser = ViewBag.adviser as AdviserInfo;}
        @Html.Hidden("hid_ID", adviser.AdviserID)
        顾问姓名:
        @Html.TextBox("txt_AdviserName", adviser.AdviserName, new { size = 50, @class = "easyui-validatebox", required = "true" })
        <br />
        顾问简介:
        <br />
        @Html.UEditor("txt_AdviderAbstract", adviser.Abstract)
        <br />
        @Html.Uploader("file_uploader", "onUploadSuccess")
        <div id="AttarchmentDIV">
            @*参照Add页面附件 html 拼装逻辑，请先理解下Add*@
            @foreach (var item in adviser.AttachmentList)
            {
                <div id='@item.FileID' fileName = '@item.FileName' saveName = '@item.SaveName'>
                    @item.FileName<a href="#" onclick="removeAttarchment('@item.FileID')">删除</a>
                </div>
            }
        </div>
        <p>
            <input type="button" value="修改" onclick="submit()" />
            <input type="button" value="返回" onclick="goback()" />
        </p>
    </div>
</body>
